﻿using LiveChartsCore.SkiaSharpView.Extensions;
using LiveChartsCore;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LiveChartsCore.SkiaSharpView.Painting;
using SkiaSharp;
using DBMode;
using Prism.Regions;
using LiveChartsCore.Drawing;
using LiveChartsCore.SkiaSharpView.Drawing;
using LiveChartsCore.SkiaSharpView;
using LiveChartsCore.Defaults;
using System.Collections.ObjectModel;
using CMS.Mode;
using Prism.Commands;

namespace CMS.ViewModels
{
    public class IndexViewModel:BindableBase,INavigationAware
    {
        private readonly IMethods<Commodity> commodityMethods;
        private readonly IMethods<Record> recordmethods;
        private readonly IRegionManager regionManager;

        public IndexViewModel(IMethods<Commodity> commodityMethods,IMethods<Record> recordmethods,IRegionManager regionManager)
        {
            this.commodityMethods = commodityMethods;
            this.recordmethods = recordmethods;
            this.regionManager = regionManager;
            TextPaint = new SolidColorPaint() { Color=SKColors.DarkBlue,SKTypeface=SKFontManager.Default.MatchCharacter('汉')};
            TextPaint2 = new SolidColorPaint() { Color = SKColors.DarkBlue, SKTypeface = SKFontManager.Default.MatchCharacter('汉') };
        }
        private List<Commodity> commoditys = new List<Commodity>();

        public List<Commodity> Commoditys
        {
            get { return commoditys; }
            set { commoditys = value; RaisePropertyChanged(); }
        }

        private IEnumerable<ISeries> series;

         public IEnumerable<ISeries> Series
        {
            get { return series; }
            set { series = value;RaisePropertyChanged(); }
        }

        private ISeries[] series_s;
        //纵坐标的值
        public ISeries[] Series_s
        {
            get { return series_s; }
            set { series_s = value; RaisePropertyChanged(); }
        }

        private  Axis[] xAxes;

        public  Axis[] XAxes
        {
            get { return xAxes; }
            set { xAxes = value; RaisePropertyChanged(); }
        }

        private Axis[] yAxes;

        public Axis[] YAxes
        {
            get { return yAxes; }
            set { yAxes = value; RaisePropertyChanged(); }
        }
        /// <summary>
        /// 所有的商品名称
        /// </summary>
        List<string> _namesList=new List<string>();

        private IPaint<SkiaSharpDrawingContext> textPaint;

        public IPaint<SkiaSharpDrawingContext> TextPaint
        {
            get { return textPaint; }
            set { textPaint = value; }
        }


        private IPaint<SkiaSharpDrawingContext> textPaint2;

        public IPaint<SkiaSharpDrawingContext> TextPaint2
        {
            get { return textPaint2; }
            set { textPaint2 = value; }
        }
        /// <summary>
        /// 所有的商品数量
        /// </summary>
        List<int> _countList = new List<int>();
        private void Refresh()
        {
            _namesList.Clear();
            _countList.Clear();
            IndexBars.Clear();
            Commoditys = commodityMethods.Query();
            Commoditys.ForEach(c =>
            {
                int intput = recordmethods.Query().FindAll(i => i.ComName == c.Name && i.Type == true).Sum(i => i.Number);
                int output = recordmethods.Query().FindAll(i => i.ComName == c.Name && i.Type == false).Sum(i => i.Number);
                c.Sum = intput - output;
                _namesList.Add(c.Name);
                _countList.Add(c.Sum);
            });

            int _index = 0;
            string[] _names = _namesList.ToArray();
            //饼状
            Series = _countList.ToArray().AsPieSeries((value, series) =>
                    {
                        series.Name = _names[_index++ % _names.Length];
                        series.DataLabelsPosition = LiveChartsCore.Measure.PolarLabelsPosition.Outer;
                        series.DataLabelsSize = 15;
                                
                        series.ToolTipLabelFormatter = point => $"{point.StackedValue.Share:P2}";
                    });

            //柱状
            {
                Series_s = new ISeries[] { new ColumnSeries<int> { Values = _countList.ToArray() } };
                XAxes = new Axis[] { new Axis
            {

                LabelsPaint= new SolidColorPaint() { Color=SKColors.DarkBlue,SKTypeface=SKFontManager.Default.MatchCharacter('汉')},
                Labels = _namesList.ToArray(),
                LabelsRotation = 0,
            } };

                YAxes = new Axis[] {  new Axis
            {
            Name = "数量", NamePaint= new SolidColorPaint() { Color=SKColors.DarkBlue,SKTypeface=SKFontManager.Default.MatchCharacter('汉')},
            NamePadding = new LiveChartsCore.Drawing.Padding(0, 15),

            LabelsPaint = new SolidColorPaint
            {
                Color = SKColors.Blue,
                FontFamily = "Times New Roman",

            },
        } };

            }

            IndexBars.Add(new IndexBar() {View= "RecordView",Icon= "\xe994" ,BackGround= "#29b6f6",Content="库存总量" });
            IndexBars.Add(new IndexBar() {View= "CommodityView", Icon= "\xe997", BackGround= "#009688", Content="商品总数" });
            IndexBars[0].Number = _countList.Sum();
            IndexBars[1].Number = Commoditys.Count;
        }

       
        private ObservableCollection<IndexBar> indexBars = new ObservableCollection<IndexBar>();

        public  ObservableCollection<IndexBar> IndexBars
        {
            get { return indexBars; }
            set { indexBars = value; RaisePropertyChanged(); }
        }

        public DelegateCommand<object> OpenCommand { get { return new DelegateCommand<object>(Open); } }

        private void Open(object obj)
        {
            if (obj is IndexBar index)
            {
                regionManager.Regions["MainViewRegion"].RequestNavigate(index.View.ToString());
            }
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            Refresh();
        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
            
        }
    }
}



